package de.lns.render;

import java.nio.*;

import org.lwjgl.BufferUtils;

import de.lns.entities.Entity;
import de.lns.level.Map;
import de.lns.tick.UpdateThread;


public class ModelFactory {
	/**Make the constructor invisible.
	 * Since this is a static-access class,
	 * it shouldn't be instantiated.
	 */
	private ModelFactory(){}
	
	private static final int rectCapacity = 12;
	private static final FloatBuffer rectMap;
	static{
		rectMap = BufferUtils.createFloatBuffer(rectCapacity);
		rectMap.put(new float[]{0f, 1f,
					1f, 0f,
					0f, 0f,
					0f, 1f,
					1f, 1f,
					1f, 0f});
		rectMap.flip();
		rectMap.mark();
	}
	
	public static FloatBuffer genRectMesh(float width, float height) {
		FloatBuffer buffer = BufferUtils.createFloatBuffer(rectCapacity);
		height /= 2;
		width /= 2;
		buffer.put(-width).put(-height).put(width).put(height).put(-width).put(height);
		buffer.put(-width).put(-height).put(width).put(-height).put(width).put(height);
		buffer.flip();
		return buffer;
	}
	
	public static FloatBuffer genRectMesh(float width, float height, float xOffset, float yOffset) {
		FloatBuffer buffer = BufferUtils.createFloatBuffer(rectCapacity);
		height /= 2;
		width /= 2;
		buffer.put(-width+xOffset).put(-height+yOffset).put(width+xOffset).
			put(height+yOffset).put(-width+xOffset).put(height+yOffset).
			put(-width+xOffset).put(-height+yOffset).put(width+xOffset).
			put(-height+yOffset).put(width+xOffset).put(height+yOffset);
		buffer.flip();
		return buffer;
	}
	
	public static FloatBuffer genRectMap() {
		FloatBuffer buffer = BufferUtils.createFloatBuffer(rectCapacity).put(rectMap);
		rectMap.reset();
		buffer.flip();
		return buffer;
	}
	
	public static FloatBuffer genRectMap(int animStates) {
		float part = 1.0f/animStates;
		FloatBuffer buffer = BufferUtils.createFloatBuffer(rectCapacity*animStates);
		int j;
		for(int i = 0; i<animStates; i++) {
			j = i+1;
			buffer.put(i*part).put(0).put(j*part).put(1).put(i*part).put(1).
			put(i*part).put(0).put(j*part).put(0).put(j*part).put(1);
		}
		buffer.flip();
		return buffer;
	}
	
	public static Model genModelSimple(Drawable d) {
		if(d.getMesh()==null || d.getMapping()==null)
			UpdateThread.kill("Missing Buffers!", new Exception());
		Model m = new Model(d, 1);
		m.addLayer(new StaticLayer(m, d.getMesh(), d.getMapping()));
		return m;
	}
	
}
